package br.com.usjt.locadoraweb.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import br.com.usjt.locadoraweb.model.AcessoBD;
import br.com.usjt.locadoraweb.model.VehicleGroup;

public class VehicleGroupDAO {

	private VehicleGroup group;
	boolean status = false;
	List<VehicleGroup> vehicleGroup;
	
	public List<VehicleGroup> search() throws Exception{
		System.out.println("passou search de VehicleDAO");
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		vehicleGroup = new ArrayList<VehicleGroup>();

		try {
			String sql = "SELECT * FROM vehiclegroup ORDER BY id";
			// sql = sql + this.montaClausulawWhere();

			AcessoBD bd = new AcessoBD();
			con = bd.obtemConexao();
			stmt = con.prepareStatement(sql);

			rs = stmt.executeQuery();
			while (rs.next()) {
	
				group = new VehicleGroup();
				group.setId(rs.getInt("id"));
				group.setGroupName(rs.getString("groupname"));
				vehicleGroup.add(group);
			}
			
			System.out.println(vehicleGroup.toString());
			return vehicleGroup;

		} catch (SQLException sq) {
			sq.printStackTrace();
			return new ArrayList<VehicleGroup>();

		} finally {
			// IMPORTANTE UTILIZACAO DO FINALLY PARA GARANTIR O FECHAMENTO
			// DA CONEXAO COM O BANCO DE DADOS.

			// fecha o resultset
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					//
				}
			}
			// fecha o statement
			//
			if (stmt != null) {
				try {
					stmt.close();
				} catch (SQLException e) {
					//
				}
			}
			// fecha a conexao
			//
			if (con != null) {
				try {
					con.close();
				} catch (SQLException e) {
					status = false;
				}
			}
		}

	}
	


}
